package com.demo.hotel_book.controller;

import com.demo.hotel_book.service.HotelServiceImpl;
import com.demo.hotel_book.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;


@Controller
public class HotelController {

    String H_Text="热度从高到低";
    String P_Text="价格从低到高";
    String param="";
    String timeIn=null;
    String timeOut=null;
    List<Boolean> stars=new ArrayList<>();

    @Autowired
    private HotelServiceImpl hotelService;

    @RequestMapping("/index2")
    public String index2(String param,String in,String out){
        this.param=param;
//        System.out.println("in="+in+"out="+out);
        //让其他controller可以获取搜索关键字，用于rooms和details页面可以保留搜索记录
        SessionUtil.getSession().setAttribute("param",param);
        SessionUtil.getSession().setAttribute("timeOut",out);
        SessionUtil.getSession().setAttribute("timeIn",in);
       timeIn=in;
       timeOut=out;
        return "index2";
    }


    @RequestMapping("/select")
    public String selectByid( Model model, HttpServletResponse response){
        model.addAttribute("timeIn",timeIn);
        model.addAttribute("timeOut",timeOut);
        //初始化酒店等级筛选状态为未选
        stars.add(0,false);
        stars.add(1,false);
        stars.add(2,false);
        stars.add(3,false);
        stars.add(4,false);
        stars.add(5,false);
        //酒店等级筛选状态更新到session
        SessionUtil.getSession().setAttribute("stars",stars);
        //滑块min和max
        SessionUtil.getSession().setAttribute("start","0");
        SessionUtil.getSession().setAttribute("end","1100");
        //酒店等级筛选初始状态6，为未选择
        SessionUtil.getSession().setAttribute("star","6");

        model.addAttribute("start",SessionUtil.getSession().getAttribute("start"));
        model.addAttribute("end",SessionUtil.getSession().getAttribute("end"));
        model.addAttribute("star",SessionUtil.getSession().getAttribute("star"));
        model.addAttribute("star5",stars.get(5));
        model.addAttribute("star4",stars.get(4));
        model.addAttribute("star3",stars.get(3));
        model.addAttribute("star2",stars.get(2));
        model.addAttribute("star1",stars.get(1));
        model.addAttribute("star0",stars.get(0));
        model.addAttribute("H_Text",H_Text);
        model.addAttribute("P_Text",P_Text);
        model.addAttribute("param1",this.param);
        model.addAttribute("buttonHide","block");
        Cookie cookie1 = new Cookie("h_flag","true");
        //cookie.setPath("/");
        cookie1.setMaxAge(60 * 60 * 24);
        response.addCookie(cookie1);
        Cookie cookie2 = new Cookie("p_flag","true");
        cookie2.setMaxAge(60 * 60 * 24);
        response.addCookie(cookie2);

        if (hotelService.SearchKey(this.param,model))
            return "NoResult";
        return "index2";//"redirect:/hotel/select"
    }









    @RequestMapping("/HeatSortAsc")
    public String heat_sort_Asc(Model model){
//        System.out.println(hotelService.HeatSortAsc());
        model.addAttribute("hotels",hotelService.HeatSortAsc());
        H_Text="热度从低到高";
        model.addAttribute("H_Text",H_Text);
        model.addAttribute("P_Text",P_Text);
        model.addAttribute("start",SessionUtil.getSession().getAttribute("start"));
        model.addAttribute("end",SessionUtil.getSession().getAttribute("end"));
        model.addAttribute("star",SessionUtil.getSession().getAttribute("star"));

        model.addAttribute("param1",param);

        model.addAttribute("timeIn",timeIn);
        model.addAttribute("timeOut",timeOut);

        List<Boolean> stars = (List<Boolean>) SessionUtil.getSession().getAttribute("stars");
        for (int i=0;i<6;i++){
            model.addAttribute("star"+i,stars.get(i));
        }

        return "index2";
    }

    @RequestMapping("/PriceSortAsc")
    public String price_sort_Asc(Model model){
//        System.out.println(hotelService.PriceSortAsc());
        model.addAttribute("hotels",hotelService.PriceSortAsc());
        P_Text="价格从低到高";
        model.addAttribute("P_Text",P_Text);
        model.addAttribute("H_Text",H_Text);
        model.addAttribute("start",SessionUtil.getSession().getAttribute("start"));
        model.addAttribute("end",SessionUtil.getSession().getAttribute("end"));
        model.addAttribute("star",SessionUtil.getSession().getAttribute("star"));

        model.addAttribute("param1",param);

        model.addAttribute("timeIn",timeIn);
        model.addAttribute("timeOut",timeOut);
        List<Boolean> stars = (List<Boolean>) SessionUtil.getSession().getAttribute("stars");
        for (int i=0;i<6;i++){
            model.addAttribute("star"+i,stars.get(i));
        }

        return "index2";
    }

    @RequestMapping("/HeatSortDes")
    public String heat_sort_Des(Model model){
//        System.out.println(hotelService.HeatSortDes());
        model.addAttribute("hotels",hotelService.HeatSortDes());
        H_Text="热度从高到低";
        model.addAttribute("H_Text",H_Text);
        model.addAttribute("P_Text",P_Text);
        model.addAttribute("start",SessionUtil.getSession().getAttribute("start"));
        model.addAttribute("end",SessionUtil.getSession().getAttribute("end"));
        model.addAttribute("star",SessionUtil.getSession().getAttribute("star"));
        model.addAttribute("param1",param);


        model.addAttribute("timeIn",timeIn);
        model.addAttribute("timeOut",timeOut);

        List<Boolean> stars = (List<Boolean>) SessionUtil.getSession().getAttribute("stars");
        for (int i=0;i<6;i++){
            model.addAttribute("star"+i,stars.get(i));
        }
        return "index2";
    }

    @RequestMapping("/PriceSortDes")
    public String price_sort_Des(Model model){
//        System.out.println(hotelService.PriceSortDes());
        model.addAttribute("hotels",hotelService.PriceSortDes());
        P_Text="价格从高到低";
        model.addAttribute("P_Text",P_Text);
        model.addAttribute("H_Text",H_Text);
        model.addAttribute("start",SessionUtil.getSession().getAttribute("start"));
        model.addAttribute("end",SessionUtil.getSession().getAttribute("end"));
        model.addAttribute("star",SessionUtil.getSession().getAttribute("star"));
        model.addAttribute("param1",param);

        model.addAttribute("timeIn",timeIn);
        model.addAttribute("timeOut",timeOut);

        List<Boolean> stars = (List<Boolean>) SessionUtil.getSession().getAttribute("stars");
        for (int i=0;i<6;i++){
            model.addAttribute("star"+i,stars.get(i));
        }

        return "index2";
    }

    @RequestMapping("/screen")
    public String screen(Model model,@RequestParam("start")String start,@RequestParam("end") String end,@RequestParam("star")Integer star){
    //从session获取酒店等级状态
        List<Boolean> stars = (List<Boolean>) SessionUtil.getSession().getAttribute("stars");
    //更新酒店等级筛选状态
       if (star!=6) {
//           System.out.println(!stars.get(star));
           this.stars.set(star, !stars.get(star));
           SessionUtil.getSession().setAttribute("stars",stars);
       }
       //更新网页的酒店筛选状态
        for (int i=0;i<6;i++){
            model.addAttribute("star"+i,stars.get(i));
        }

        model.addAttribute("timeIn",timeIn);
        model.addAttribute("timeOut",timeOut);

        SessionUtil.getSession().setAttribute("start",start);
        SessionUtil.getSession().setAttribute("end",end);
        SessionUtil.getSession().setAttribute("star",star);
        model.addAttribute("param1",param);
        model.addAttribute("start",SessionUtil.getSession().getAttribute("start"));
        model.addAttribute("end",SessionUtil.getSession().getAttribute("end"));
        model.addAttribute("star",SessionUtil.getSession().getAttribute("star"));
        model.addAttribute("P_Text",P_Text);
        model.addAttribute("H_Text",H_Text);
        model.addAttribute("hotels", hotelService.screen(start,end,stars,param));
        hotelService.setListHotel(hotelService.screen(start,end,stars,param));
        return "index2";
    }


}
